package com.handu.luna.boss.controller;


import com.handu.luna.core.domain.system.DeliverArea;
import com.handu.luna.core.domain.system.Region;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by wangzailiang on 2014/7/17.
 */
@Controller
@RequestMapping("/system/deliverarea/*")
public class DeliverAreaController extends BaseController {

    @RequestMapping
    public BossModel index() {

        return createModel("/system/deliverarea/index")
                .add("deliverAreaList",context.getDeliverAreaRepository().findAll());
    }

    @RequestMapping(value = "add",method = RequestMethod.GET)
    public BossModel add() {

        return getEditModel(new DeliverArea());
    }

    @RequestMapping(value = "edit/{id}",method = RequestMethod.GET)
    public BossModel edit(@PathVariable long id) {

        return getEditModel(context.getDeliverAreaRepository().findOne(id));
    }


    @InitBinder(value = {"name","description","price","regionIds","filterRegionIds","filterable"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public BossModel save(@Valid DeliverArea deliverArea, BindingResult bindingResult, RedirectAttributes redirectAttributes) {

        if(!deliverArea.isFilterable()) {
            deliverArea.setFilterRegionIdList(null);
        }

        if(null == deliverArea.getRegionIdList()) {
            bindingResult.rejectValue("regionIdList","","所属区域不能为空");
        }

        if(deliverArea.isFilterable() && deliverArea.getFilterRegionIdList() == null) {
            bindingResult.rejectValue("filterRegionIdList","","请选择要过滤的地区");
        }

        if(bindingResult.hasErrors()) {
            deliverArea.init();
            return getEditModel(deliverArea);
        }

        if(deliverArea.isNew()) {
            context.getDeliverAreaRepository().save(deliverArea);
            redirectAttributes.addFlashAttribute("message","添加 配送区域 成功");
        }else {
            deliverArea.update();
            redirectAttributes.addFlashAttribute("message","修改 配送区域 成功");
        }


        return createModel("redirect:index");
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public BossModel delete(long id){

        context.getDeliverAreaRepository().delete(id);

        return createModel("/system/deliverarea/deliverarea_table")
                .add("deliverAreaList", context.getDeliverAreaRepository().findAll());
    }

    @RequestMapping(value = "searchRegion", produces = {"application/json;charset=UTF-8"})
    public @ResponseBody String searchRegion(long parentId) {

        System.out.println(parentId);
        List<Region> regionList = context.getRegionRepository().findRegionListByParentId(parentId);
        return JSONArray.fromObject(regionList).toString();

    }

    private BossModel getEditModel(DeliverArea deliverArea) {

        BossModel bossModel = createModel("/system/deliverarea/info").add("deliverArea", deliverArea);
        if(deliverArea.isNew()) {
            bossModel.add("provinceList", context.getRegionRepository().findRegionListByParentId(1));
        }else {
            bossModel.add("provinceList",deliverArea.filterRegions());
        }
        return bossModel;
    }


}
